//
//  KiwipenCommand.m
//  BlueToothDemo
//
//  Created by US bike on 2017/8/29.
//  Copyright © 2017年 奇异果. All rights reserved.
//

#import "KiwipenCommand.h"
#import "KiwipenProtocol.h"
#import "ToolMethods.h"

@implementation KiwipenCommand

+ (instancetype) CreateCommondWitCommandType:(CommandType) type AndDeviceIdStr:(NSString *) deviceIDStr {
    KiwipenCommand *cmd = [KiwipenCommand new];
    cmd.commandType = type;
    if (type == CommandTypePower) {
        Byte bytes[5] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_POWER, CMD_OPERATOR_POWER, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:5];
    } else if (type == CommandTypeReadUUID) {
        Byte bytes[5] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_UUID, CMD_OPERATOR_UUID, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:5];
    } else if (type == CommandTypeWriteUUID) {
        Byte *bytes = [self transfromStrToBytesWithStr:deviceIDStr];
        cmd.commandData = [NSData dataWithBytes:bytes length:22];
        
    } else if (type == CommandTypeLED_ALL_RESET) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_RESET, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_RESET, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else if (type == CommandTypeLED_ALL_OFF) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_OFF, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_OFF, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else if (type == CommandTypeLED_ALL_ON) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_ON, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_ON, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else if (type == CommandTypeLED_ALL_SLOW_FLASHING) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_SLOW_FLASHING, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_SLOW_FLASHING, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else if (type == CommandTypeLED_ALL_FAST_FLASHING) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_FAST_FLASHING, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_FAST_FLASHING, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else if (type == CommandTypeLED_ALL_BREATHING) {
        Byte crc = [ToolMethods xorsBytes:CMD_OPERATOR_LED, LED_ALL,LED_STATUS_BREATHING, nil];
        Byte bytes[7] = {CMD_Begin1, CMD_Begin2, CMD_OPERATOR_LED, LED_ALL, LED_STATUS_BREATHING, crc, CMD_END};
        cmd.commandData = [NSData dataWithBytes:bytes length:7];
    } else {
        cmd.commandType = CommandTypeError;
    }
    return cmd;
}


+ (Byte *) transfromStrToBytesWithStr:(NSString *) deviceId {
    
    NSData *data = [self DataFromHexStr:deviceId];
    
    Byte *dataBytes = (Byte *)(data.bytes);
    Byte length = data.length & 0xFF;
    
    
    Byte crc = 0x00;
    for (int i = 0; i < data.length; i++) {
        crc = [ToolMethods xorByteA:crc AndByteB:dataBytes[i]];
    }
    crc = [ToolMethods xorsBytes:CMD_OPERATOR_UUID, length, crc, nil];
    
    
    Byte *bytes = malloc(sizeof(Byte) * 22);
    bytes[0] = CMD_Begin1;
    bytes[1] = CMD_Begin2;
    bytes[2] = CMD_OPERATOR_UUID;
    bytes[3] = length;
    bytes[4] = dataBytes[0];//数据
    bytes[5] = dataBytes[1];
    bytes[6] = dataBytes[2];
    bytes[7] = dataBytes[3];
    bytes[8] = dataBytes[4];
    bytes[9] = dataBytes[5];
    bytes[10] = dataBytes[6];
    bytes[11] = dataBytes[7];
    bytes[12] = dataBytes[8];
    bytes[13] = dataBytes[9];
    bytes[14] = dataBytes[10];
    bytes[15] = dataBytes[11];
    bytes[16] = dataBytes[12];
    bytes[17] = dataBytes[13];
    bytes[18] = dataBytes[14];
    bytes[19] = dataBytes[15];
    bytes[20] = crc;//校验位
    bytes[21] = CMD_END;
    
    
    return bytes;
    
}

- (NSString *) commandTypeName {
    NSString *typeStr = @"错误类型";
    switch (self.commandType) {
        case CommandTypeError:
            typeStr = @"错误类型";
            break;
        case CommandTypePower:
            typeStr = @"获取电池电量";
            break;
        case CommandTypeReadUUID:
            typeStr = @"获取device_id";
            break;
        case CommandTypeWriteUUID:
            typeStr = @"写入device_id";
            break;
            
        case CommandTypeLED_ALL_RESET:
            typeStr = @"LED_ALL_RESET";
            break;
        case CommandTypeLED_ALL_OFF:
            typeStr = @"LED_ALL_OFF";
            break;
        case CommandTypeLED_ALL_ON:
            typeStr = @"LED_ALL_ON";
            break;
        case CommandTypeLED_ALL_SLOW_FLASHING:
            typeStr = @"LED_ALL_SLOW_FLASHING";
            break;
        case CommandTypeLED_ALL_FAST_FLASHING:
            typeStr = @"LED_ALL_FAST_FLASHING";
            break;
        case CommandTypeLED_ALL_BREATHING:
            typeStr = @"LED_ALL_SLOW_BREATHING";
            break;
        default:
            break;
    }
    return typeStr;
}

//把十六进制转为NSData
+ (NSData *) DataFromHexStr:(NSString *) hexString {
    
    
    hexString = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];//去掉空格
    
    
    const char *hexChar = [hexString UTF8String];//转换为 char 字符串
    
    Byte *bt = malloc(sizeof(Byte)*(hexString.length/2));// 开辟空间 用来存放转换后的byte
    
    char tmpChar[3] = {'\0','\0','\0'};
    
    int btIndex = 0;
    
    for (int i=0; i<hexString.length; i += 2) {
        
        tmpChar[0] = hexChar[i];
        
        tmpChar[1] = hexChar[i+1];
        
        bt[btIndex] = strtoul(tmpChar, NULL, 16);// 将 hexstring 转换为 byte 的c方法16 为16进制
        
        btIndex ++;
        
    }
    
    NSData *data = [NSData dataWithBytes:bt length:btIndex]; //创建 nsdata 对象
    
    free(bt);//释放空间
    
    return data;
    
    
}


- (NSString *) commandDetail {
    return [NSString stringWithFormat:@"%@",_commandData];
}

- (NSString *) description {
    return [NSString stringWithFormat:@"KiwipenCommand: type = %@, data = %@",[self commandTypeName], _commandData];
}
@end
